#############################################################
#
# sane-backend
#
#############################################################

SANE_BACKENDS_VERSION=1.0.18
SANE_BACKENDS_SOURCE=sane-backends-$(SANE_BACKENDS_VERSION).tar.gz
SANE_BACKENDS_SITE=http://alioth.debian.org/frs/download.php/1669
SANE_BACKENDS_DIR=$(BUILD_DIR)/${shell basename $(SANE_BACKENDS_SOURCE) .tar.gz}
SANE_BACKENDS_WORKDIR=$(BUILD_DIR)/sane-backends-$(SANE_BACKENDS_VERSION)
SANE_BACKENDS_CAT:=zcat

$(DL_DIR)/$(SANE_BACKENDS_SOURCE):
	$(WGET) -P $(DL_DIR) $(SANE_BACKENDS_SITE)/$(SANE_BACKENDS_SOURCE)

$(SANE_BACKENDS_DIR)/.unpacked:	$(DL_DIR)/$(SANE_BACKENDS_SOURCE)
	$(SANE_BACKENDS_CAT) $(DL_DIR)/$(SANE_BACKENDS_SOURCE) | tar -C $(BUILD_DIR) $(TAR_OPTIONS) -
	touch $(SANE_BACKENDS_DIR)/.unpacked

$(SANE_BACKENDS_DIR)/.patched: $(SANE_BACKENDS_DIR)/.unpacked
	(cd $(SANE_BACKENDS_DIR); QUILT_PATCHES=$(BASE_DIR)/package/sane-backends $(QUILT) push -a)
	echo $(BASE_DIR)/package/sane-backends > $@

$(SANE_BACKENDS_DIR)/.configured: $(SANE_BACKENDS_DIR)/.patched
	(cd $(SANE_BACKENDS_DIR); rm -rf config.cache; \
		$(TARGET_CONFIGURE_OPTS) \
		CFLAGS="$(TARGET_CFLAGS)" \
		LIB_USB_PATH=$(STAGING_DIR)/lib/libusb.so \
		./configure \
		--target=$(GNU_TARGET_NAME) \
		--host=$(GNU_TARGET_NAME) \
		--build=$(GNU_HOST_NAME) \
		--prefix=/usr \
		--sysconfdir=/etc \
		--with-lockdir=/var/lock \
		--disable-translations \
		--without-gphoto2 \
		--with-pic \
	);
	touch  $(SANE_BACKENDS_DIR)/.configured

$(SANE_BACKENDS_WORKDIR)/backend/.libs/libsane.so.$(SANE_BACKENDS_VERSION): $(SANE_BACKENDS_DIR)/.configured
	$(MAKE) CC=$(TARGET_CC) BUILDROOT_HOST_CC=$(HOSTCC) -C $(SANE_BACKENDS_WORKDIR)
	touch $@

$(STAGING_DIR)/usr/lib/libsane.so.$(SANE_BACKENDS_VERSION): $(TARGET_DIR)/lib/libusb.la $(SANE_BACKENDS_WORKDIR)/backend/.libs/libsane.so.$(SANE_BACKENDS_VERSION)
	$(MAKE) prefix=$(STAGING_DIR)/usr -C $(SANE_BACKENDS_WORKDIR) install
	touch $@

$(TARGET_DIR)/usr/lib/libsane.so.$(SANE_BACKENDS_VERSION): $(STAGING_DIR)/usr/lib/libsane.so.$(SANE_BACKENDS_VERSION)
	$(MAKE) prefix=$(TARGET_DIR)/usr sysconfdir=$(TARGET_DIR)/etc -C $(SANE_BACKENDS_WORKDIR) install
	touch $@

sane-backends:	uclibc $(TARGET_DIR)/usr/lib/libsane.so.$(SANE_BACKENDS_VERSION)

sane-backends-source: $(DL_DIR)/$(SANE_BACKENDS_SOURCE)

sane-backends-clean:
	@if [ -d $(SANE_BACKENDS_WORKDIR)/Makefile ] ; then \
		$(MAKE) -C $(SANE_BACKENDS_WORKDIR) clean ; \
	fi;

sane-backends-dirclean:
	rm -rf $(SANE_BACKENDS_DIR) $(SANE_BACKENDS_WORKDIR)

#############################################################
#
# Toplevel Makefile options
#
#############################################################
ifeq ($(strip $(BR2_PACKAGE_SANE_BACKENDS)),y)
TARGETS+=sane-backends
endif
